﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Caching;
using Netkk.Common;
using Netkk.Common.Interfaces.Repositories;
using Netkk.Common.Models;
using System.Xml.Linq;

namespace Netkk.Data.Repositories
{
    public class AppointmentRepository : BaseRepository, IAppointmentRepository
    {
        private readonly string appointmentPath;

        protected XDocument AppointmentDocument
        {
            get { return CacheWrapper.Load("appointments", new CacheDependency(appointmentPath), () => XDocument.Load(appointmentPath)); }
        }

        public AppointmentRepository()
        {
            appointmentPath = string.Concat(ConnectionString, "Appointments.xml");
        }

        public IEnumerable<Appointment> Select()
        {
            IEnumerable<Appointment> appointments = from c in AppointmentDocument.Element("Appointments").Elements("Appointment")
                                                    select getAppointmentFromXElement(c);
            return appointments;
        }

        /// <summary>
        /// Gets the appointment from X element.
        /// </summary>
        /// <param name="XAppointment">The X appointment.</param>
        /// <returns></returns>
        private Appointment getAppointmentFromXElement(XElement XAppointment)
        {
            return new Appointment
            {
                Id = XAppointment.Element("Id").Value.ToOrDefault<Guid>(),
                Title = XAppointment.Element("Title").Value,
                ShortTitle = XAppointment.Element("ShortTitle").Value,
                Date = XAppointment.Element("Date").Value.ToOrDefault<DateTime>(),
                Location = XAppointment.Element("Location").Value,
                Teaser = XAppointment.Element("Teaser").Value,
                Description = XAppointment.Element("Description").Value
            };
        }

        public void Delete(Appointment appointment)
        {
            XElement xcontent = (from c in AppointmentDocument.Element("Appointments").Elements("Appointment")
                                 where c.Element("Id").Value.ToOrDefault<Guid>() == appointment.Id
                                 select c).FirstOrDefault();
            if (xcontent != null)
            {
                xcontent.Remove();
                AppointmentDocument.Save(appointmentPath);
            }
        }

        public void Save(Appointment appointment)
        {
            XElement xcontent = (from c in AppointmentDocument.Element("Appointments").Elements("Appointment")
                                 where c.Element("Id").Value.ToOrDefault<Guid>() == appointment.Id
                                 select c).FirstOrDefault();
            if (xcontent == null)
            {
                xcontent = new XElement("Appointment");
                AppointmentDocument.Element("Appointments").Add(xcontent);
            }
            else
            {
                xcontent.RemoveAll();
            }

            xcontent.Add(
                new XElement("Id") { Value = appointment.Id.ToString("B") },
                new XElement("Title") { Value = appointment.Title },
                new XElement("ShortTitle") { Value = appointment.ShortTitle },
                new XElement("Date") { Value = appointment.Date.ToString("dd.MM.yyyy HH:mm:ss") },
                new XElement("Location") { Value = appointment.Location },
                new XElement("Teaser") { Value = appointment.Teaser },
                new XElement("Description") { Value = appointment.Description });

            AppointmentDocument.Save(appointmentPath);
        }

    }

}
